package flexibility;

import gui.secretaryScreen;

import java.sql.SQLException;

import javax.swing.JList;

import com.sun.rowset.CachedRowSetImpl;

import basic.ClientConsole;

public class messages implements Runnable {

	private userData msgData = new userData();
	private CachedRowSetImpl crs;
	private boolean haveMessages = false;
	private ClientConsole connection;
	private serverIP server = new serverIP();
	private int hasChanged = 0;

	public messages(int userID) {// this is the constructor according to the
		
		server.getServerConf();
		connection = new ClientConsole(server.host, server.port); // ID of the
																	// user
		msgData.addData(4);
		msgData.addData(1);
		msgData.addData(userID);// this is for the the userID

	}

	public void getNewMessages() {// this will query data base for new messages

		connection.acceptMsg(msgData);
		crs = connection.getResults();
		hasChanged = 1;
		
		try {
			if (crs.next() == true) {
				haveMessages = true;

			} else {
				haveMessages = false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void createMessage() {

	}

	public boolean checkForMessages() {// this will invoke the get messages from
		// database method
		getNewMessages();
		if (haveMessages) {

			return true;
		}

		return false;
	}
	
	public boolean newMsgFlag(){
		return haveMessages;
	}

	public CachedRowSetImpl getMessages(){
		while(hasChanged==0){
			
		}
		return crs;
		
	}
	public void run() {
		while (true) {

			checkForMessages();

			if (haveMessages == true) {
				// add here the code to show msg
			}
			try {

				Thread.sleep(5000);

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
